//
//  GYZCountryViewController.m
//  Qyer
//
//  Created by GYz on 15/10/19.
//  Copyright © 2015年 GF. All rights reserved.
//

#import "GYZCountryViewController.h"
#import "BaseHeader.h"
@interface GYZCountryViewController () <UITableViewDataSource, UITableViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong)NSMutableArray *photoUrlArr;
@property (nonatomic, copy)NSString *cnname;
@property (nonatomic, copy)NSString *enname;
@property(nonatomic, copy)NSString *entryCont;
@property(nonatomic,retain)UIImageView *backView;
@property(nonatomic, retain)UILabel *entryContLab ;
@property(nonatomic, retain)UILabel *ennameLab;
@property(nonatomic, retain)UILabel *cnnameLab;
@property (nonatomic, strong)UIView *navView;
@property (nonatomic, strong)UIScrollView *imageScroll;
@property (nonatomic, assign)NSInteger pageCount;
@property (nonatomic, strong)NSMutableArray *hotCityArr;
@property (nonatomic, strong)NSMutableArray *freeArr;
@property (nonatomic, strong)UIButton *countryDetailButton;

@end

@implementation GYZCountryViewController

- (void)dealloc
{
    
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    _pageCount = 1;
    [self afn];
}

#pragma makr - 创建假navigation
- (void)creatNavigation
{
    _navView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    _navView.backgroundColor = [UIColor whiteColor];
    _navView.alpha = 0;
    
    UILabel *name = [self creatLabelWithRect:CGRectMake(0, 10, SCREEN_WIDTH, 30) text:self.cnname font:[UIFont systemFontOfSize:15.0] textColor:[UIColor blackColor] alignment:YES];
    [_navView addSubview:name];
    
    UILabel *enname = [self creatLabelWithRect:CGRectMake(0, 30, SCREEN_WIDTH, 30) text:self.enname font:[UIFont systemFontOfSize:14.0] textColor:[UIColor grayColor] alignment:YES];
    [_navView addSubview:enname];
    
    UIButton *backButton = [self creatButtonWithRect:CGRectMake(10, 30, 24, 24) imageStr:@"whiteback.png" click:@selector(backViewClick:)];
    [self.view addSubview:backButton];
    
    UIButton *backButtonGreen = [self creatButtonWithRect:CGRectMake(10, 30, 24, 24) imageStr:@"iconfont-31fanhui1" click:@selector(backViewClick:)];
    [_navView addSubview:backButtonGreen];
    
    [self.view addSubview:_navView];

}

// 返回按钮点击方法
- (void)backViewClick:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 创建顶部轮播图
- (void)creatTopImage
{
    // 创建底部view
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT * 2 / 5)];
    backView.backgroundColor = [UIColor whiteColor];
    
    // 创建轮播图scrollView, 放在底部view上
    _imageScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT * 2 / 5)];
    _imageScroll.contentSize = CGSizeMake((_photoUrlArr.count + 2) * SCREEN_WIDTH, 0);
    _imageScroll.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
    [_imageScroll showsHorizontalScrollIndicator];
    _imageScroll.pagingEnabled = YES;
    _imageScroll.delegate = self;
    
    for (int i = 0; i < _photoUrlArr.count+2; i++) {
        UIImageView *picV = [[UIImageView alloc]initWithFrame:CGRectMake(i*SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT/5*2)];
        // 在前面加一张图,和最后一张相同
        if (i == 0) {
            [picV sd_setImageWithURL:[NSURL URLWithString:[_photoUrlArr objectAtIndex:_photoUrlArr.count-1]] placeholderImage:[UIImage imageNamed:@"zhanweiPic"]];
            
        }
        if (i>0&&i<_photoUrlArr.count+1)
        {
            [picV sd_setImageWithURL:[NSURL URLWithString:[_photoUrlArr objectAtIndex:i-1]] placeholderImage:[UIImage imageNamed:@"zhanweiPic"]];
        }
        //最后面也加一张图,和第一张一样
        if (i == _photoUrlArr.count +1)
        {
            [picV sd_setImageWithURL:[NSURL URLWithString:[_photoUrlArr objectAtIndex:0]] placeholderImage:[UIImage imageNamed:@"zhanweiPic"]];
        }
        [_imageScroll addSubview:picV];
    }
    [backView addSubview:_imageScroll];
    
    //放在轮播图上的灰色的背景图,为了让文字更清晰的显示
    _backView = [[UIImageView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT/5, SCREEN_WIDTH, SCREEN_HEIGHT/5)];
    [_backView setImage:[UIImage imageNamed:@"backViewCell"]];
    [backView addSubview:_backView];
    
    // 国家使用信息
    NSString *str = nil;
    if (self.entryCont.length > 40) {
        str = [NSString stringWithFormat:@"%@...", [self.entryCont substringToIndex:40]];
    }
    else
    {
        str = self.entryCont;
    }
    _entryContLab = [self creatLabelWithRect:CGRectMake(20, SCREEN_HEIGHT * 2 / 5 - SCREEN_HEIGHT * 0.09, SCREEN_WIDTH - 40, SCREEN_HEIGHT * 0.09) text:str font:[UIFont systemFontOfSize:14.0] textColor:[UIColor whiteColor] alignment:NO];
    _entryContLab.numberOfLines = 2;
    [backView addSubview:_entryContLab];
    
    _countryDetailButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _countryDetailButton.frame = CGRectMake(SCREEN_WIDTH - 60, SCREEN_HEIGHT * 2 / 5 - SCREEN_HEIGHT * 0.051, 40, SCREEN_HEIGHT * 0.045);
    [_countryDetailButton setTitle:@"更多>" forState:UIControlStateNormal];
    [_countryDetailButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _countryDetailButton.titleLabel.font = [UIFont systemFontOfSize:14.0];
    [_countryDetailButton addTarget:self action:@selector(countryDetailClick) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:_countryDetailButton];


    // 英文国家名
    _ennameLab = [self creatLabelWithRect:CGRectMake(20, SCREEN_HEIGHT * 2 / 5 - SCREEN_HEIGHT * 0.12, SCREEN_WIDTH, SCREEN_HEIGHT * 0.045) text:self.enname font:[UIFont systemFontOfSize:14.0] textColor:[UIColor whiteColor] alignment:NO];
    [backView addSubview:_ennameLab];
    
    // 中文国家名
    _cnnameLab = [self creatLabelWithRect:CGRectMake(20, SCREEN_HEIGHT * 2 / 5 - SCREEN_HEIGHT * 0.17, SCREEN_WIDTH, SCREEN_HEIGHT * 0.045) text:self.cnname font:[UIFont systemFontOfSize:18.0] textColor:[UIColor whiteColor] alignment:NO];
    [backView addSubview:_cnnameLab];
    
    self.tableView.tableHeaderView = backView;
    
    NSTimer *time = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(titleScrollTime) userInfo:nil repeats:YES];
}
-(void)titleScrollTime
{
    [_imageScroll setContentOffset:CGPointMake(_pageCount * SCREEN_WIDTH, 0) animated:YES];
    _pageCount++;
    
}

- (void)countryDetailClick
{
    GYZCountryDetailViewController *detail = [[GYZCountryDetailViewController alloc] init];
    detail.cid = self.cid;
    [self.navigationController pushViewController:detail animated:YES];
}

#pragma mark - scrollView代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 顶部轮播图实现
    if (scrollView == _imageScroll) {
        // 偏移量/当页宽度 = 当前第几页
        _pageCount = _imageScroll.contentOffset.x / SCREEN_WIDTH;
        if (_pageCount == _photoUrlArr.count + 1) {
            [_imageScroll setContentOffset:CGPointMake(SCREEN_WIDTH, 0) animated:NO];
        }
        if (_imageScroll.contentOffset.x <= 0) {
            [_imageScroll setContentOffset:CGPointMake(_photoUrlArr.count * SCREEN_WIDTH, 0) animated:NO];
        }
    }
    
    // 顶部轮播图下拉放大
    if (self.tableView.contentOffset.y < 0) {
        
        _imageScroll.transform = CGAffineTransformMakeScale((_imageScroll.frame.size.height-self.tableView.contentOffset.y)/_imageScroll.frame.size.height, (_imageScroll.frame.size.height-self.tableView.contentOffset.y)/_imageScroll.frame.size.height);
        _imageScroll.center = CGPointMake(SCREEN_WIDTH/2, _imageScroll.frame.size.height/2+self.tableView.contentOffset.y);
        
        _cnnameLab.frame = CGRectMake(20, _imageScroll.frame.size.height-SCREEN_HEIGHT*0.17 + self.tableView.contentOffset.y, SCREEN_WIDTH - 40, 30);
        
        _ennameLab.frame = CGRectMake(20, _imageScroll.frame.size.height - SCREEN_HEIGHT*0.12+self.tableView.contentOffset.y, SCREEN_WIDTH , SCREEN_HEIGHT*0.045);
        
        _entryContLab.frame = CGRectMake(20, _imageScroll.frame.size.height - SCREEN_HEIGHT*0.09+self.tableView.contentOffset.y, SCREEN_WIDTH - 40, SCREEN_HEIGHT*0.09);
        
        _countryDetailButton.frame = CGRectMake(SCREEN_WIDTH - 60, _imageScroll.frame.size.height - SCREEN_HEIGHT * 0.051 + self.tableView.contentOffset.y, 40, SCREEN_HEIGHT * 0.045);
        
        _backView.frame = CGRectMake(0, _imageScroll.frame.size.height/2+self.tableView.contentOffset.y, SCREEN_WIDTH, _imageScroll.frame.size.height / 2);
    }
    
    // 伪造nav隐藏/出现
    _navView.alpha = 0.01 * self.tableView.contentOffset.y;
}

#pragma mark - 网络请求&数据解析
 - (void)afn
{
    _photoUrlArr = [[NSMutableArray alloc] init];
    _hotCityArr = [[NSMutableArray alloc] init];
    _freeArr = [[NSMutableArray alloc] init];
    [AFN getdataWithString:[NSString stringWithFormat:@"http://open.qyer.com/qyer/footprint/country_detail?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&country_id=%@&lat=38.88266913415454&lon=121.5397627126178&page=1&track_app_version=6.6&track_deviceid=29DA480A-87F0-4C65-A48A-4B13DE755312", self.cid] block:^(id block) {
        
        NSDictionary *dataDic = [block objectForKey:@"data"];
        self.cnname = [dataDic objectForKey:@"cnname"];
        self.enname = [dataDic objectForKey:@"enname"];
        self.entryCont = [dataDic objectForKey:@"entryCont"];
        // 轮播图数据解析
        for (NSString *photoStr in [[block objectForKey:@"data"] objectForKey:@"photos"]) {
            [_photoUrlArr addObject:photoStr];
        }
        // 城市数据解析
        for (NSDictionary *dic in [dataDic objectForKey:@"hot_city"]) {
            GYZHot_countryModel *model = [GYZHot_countryModel modelWithDictionary:dic];
            [_hotCityArr addObject:model];
        }
        // 超值自由行数据解析
        for (NSDictionary *dic in [dataDic objectForKey:@"new_discount"]) {
            GYZFreeModel *model = [GYZFreeModel modelWithDictionary:dic];
            [_freeArr addObject:model];
        }
        
        [self creatTableView];
        [self creatNavigation];
        [self creatTopImage];
    } BLOCK:^(id BLOCK) {
    }];
}

#pragma mark - 创建tableView
- (void)creatTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}

#pragma mark - tableView代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    else
    {
        return _freeArr.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        static NSString *cellId = @"countryCollectionCell";
        GYZCountryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (!cell) {
            cell = [[GYZCountryTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        }
        cell.delegate = self;
        cell.dataSource = self;
        return cell;
    }
    else
    {
        static NSString *cellID = @"cellId";
        GYZFreeTabelViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[GYZFreeTabelViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        GYZFreeModel *model = _freeArr[indexPath.row];
        [cell setValueWithModel:model];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WebViewController *web = [[WebViewController alloc] init];
    GYZFreeModel *model = _freeArr[indexPath.row];
    NSString *string = [NSString stringWithFormat:@"http://open.qyer.com/lastminute/get_detail?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&id=%@&oauth_token=d5ecb9febb61510d54ba2d47d986cf65&page=1&source=QYCountryDetailViewController&track_user_id=6695170&v=1",model.cid];
    [AFN getdataWithString:string block:^(id block) {
        NSDictionary *dataDic = [block objectForKey:@"data"];
        NSString *urlStr = [dataDic objectForKey:@"qyer_url"];
        web.webString = urlStr;
        [self.navigationController pushViewController:web animated:YES];
    } BLOCK:^(id BLOCK) {
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 2*SCREEN_WIDTH/25 * 6.6+0.12*SCREEN_WIDTH;
    }
    else
    {
        return SCREEN_WIDTH * 3 / 12 + 20;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return SCREEN_WIDTH * 0.15;
}

// 自定义section
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH * 0.15)];
    view.backgroundColor = [UIColor whiteColor];
    
    
    if (section == 0) {
        UIButton *button = [self creatButtonWithRect:CGRectMake(SCREEN_WIDTH/3*2, SCREEN_WIDTH * 0.025, SCREEN_WIDTH/3, 0.1*SCREEN_WIDTH) imageStr:nil click:@selector(seeAllCountry:)];
        [button setTitle:@"查看全部 >" forState:UIControlStateNormal];
        [button.titleLabel setFont:[UIFont systemFontOfSize:15.0]];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        UILabel *countryLabel = [self creatLabelWithRect:CGRectMake(10, SCREEN_WIDTH * 0.025, SCREEN_WIDTH/4, 0.1*SCREEN_WIDTH) text:[NSString stringWithFormat:@"%@城市", self.cnname] font:[UIFont systemFontOfSize:15.0] textColor:[UIColor grayColor] alignment:NO];
        [view addSubview:countryLabel];
        [view addSubview:button];
    }
    else
    {
        UIButton *button = [self creatButtonWithRect:CGRectMake(SCREEN_WIDTH/3*2, SCREEN_WIDTH * 0.025, SCREEN_WIDTH/3, 0.1*SCREEN_WIDTH) imageStr:nil click:@selector(superFree:)];
        [button setTitle:@"查看全部 >" forState:UIControlStateNormal];
        [button.titleLabel setFont:[UIFont systemFontOfSize:15.0]];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        UILabel *freeLabel = [self creatLabelWithRect:CGRectMake(10, SCREEN_WIDTH * 0.025, SCREEN_WIDTH / 4, 0.1 * SCREEN_WIDTH) text:@"超值自由行" font:[UIFont systemFontOfSize:15.0] textColor:[UIColor grayColor] alignment:NO];
        [view addSubview:freeLabel];
        [view addSubview:button];
    }
    
    UILabel *headerLine = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.5)];
    headerLine.backgroundColor = [UIColor grayColor];
    
    UILabel *footerLine = [[UILabel alloc] initWithFrame:CGRectMake(0, SCREEN_WIDTH * 0.15, SCREEN_WIDTH, 0.5)];
    footerLine.backgroundColor = [UIColor grayColor];
    
    [view addSubview:headerLine];
    [view addSubview:footerLine];
    
    return view;
}

// 城市查看全部点击方法
- (void)seeAllCountry:(UIButton *)sender
{
    GYZAllCountryViewController *allCountry = [[GYZAllCountryViewController alloc] init];
    allCountry.urlStr = self.cid;
    [self.navigationController pushViewController:allCountry animated:YES];
}
// 超值自由行查看全部点击方法
- (void)superFree:(UIButton *)sender
{
    GYZSuperFreeViewController *superFree = [[GYZSuperFreeViewController alloc] init];
    superFree.cnname = self.cnname;
    // 网址里,如果是在国家页面点,type就是1,城市页面点超值自由行的查看全部,type就是2
    superFree.type = 1;
    superFree.left = YES;
    superFree.urlStr = self.cid;
    [self.navigationController pushViewController:superFree animated:YES];
}

#pragma mark - collectionView代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 4;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    GYZCountryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"countryCollectionViewCell" forIndexPath:indexPath];
    GYZHot_countryModel *model = _hotCityArr[indexPath.row];
    [cell setValueWithModel:model];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    GYZCityViewController *city = [[GYZCityViewController alloc] init];
    GYZHot_countryModel *model = _hotCityArr[indexPath.row];
    city.cid = model.cid;
    city.cnname = model.cnname;
    city.enname = model.enname;
    [self.navigationController pushViewController:city animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
